package securityauthcenter

import (
	"git.oschina.net/YPTArch/usercenter/remoteserver/models"
	"git.oschina.net/YPTArch/usercenter/remoteserver/rpcremote"
	"golang.org/x/net/context"
	"git.oschina.net/YPTArch/securityauthcenter/rpc/pd"
	"io"
)

func Create(userId,appId string) error {
	_, err := rpcremote.GetUserAuthAppClient().Create(context.Background(), &pb.UserAuthAppRequest{UserId: userId, AppId: appId})
	if err != nil {
		return err
	}
	return nil
}
func GetUserAuthApp(userId string) ([]models.UserAuthApp,error ){

	userAuthAppArr :=[]models.UserAuthApp{}
	stream,err:=rpcremote.GetUserAuthAppClient().GetUserAuthApp(context.Background(), &pb.GetUserAuthAppRequest{UserId: userId})
	if err!=nil{
		return nil,err
	}
	for{
		temp,err:=stream.Recv()
		if err==io.EOF{
			break
		}
		if err != nil {
			return nil,err
		}
		userAuthApp:= models.UserAuthApp{}
		userAuthApp.AppId=temp.AppId
		userAuthApp.UserId=temp.UserId
		userAuthAppArr=append(userAuthAppArr,userAuthApp)
	}
	return userAuthAppArr,nil
}
